﻿var HelpModel = new function () {
    this.DisplayHelp = ko.observable(false);
    this.GoBackStep1 = function () {
        UploadFileModel.DisplayFirstView(true);
        DeleteUpload();
        HelpModel.DisplayHelp(false);
        MappingModel.DisplaySecondView(false);
        ReviewImportModel.DisplayThirdView(false);
    };
    this.GoToMatch = function () {
        $(".fancybox-skin").show();
        $("#fancybox-overlay").show();
        $("#fancybox-overlay").css("opacity", "0.8");
        $.fancybox($('#popup-processing-getmapping'), {
            padding: 0,
            closeBtn: false,
            helpers: {
                overlay: {
                    opacity: 0.8,
                    css: { 'background-color': '#11619d' },
                    closeClick: false
                }
            }
        });

        var ManualImportId = $('#StoreManualID').val();

        if (ManualImportId != undefined) {

            var dataPost = { "file": ManualImportId };

            $.ajax({
                type: "POST",
                url: "/LeadImport/GetMapping",
                data: dataPost,
                dataType: "json",
                success: function (ModelFields, textStatus, jqXHR) {
                    $.fancybox.close();
                    if ((ModelFields != "") && (ModelFields.Mapping.Columns != null)) {
                        UploadFileModel.DisplayFirstView(false);
                        HelpModel.DisplayHelp(false);

                        MappingModel.AllFieldGroups(ModelFields.Mapping.AllFieldGroups);

                        MappingModel.DisplaySecondView(true);
                        ReviewImportModel.DisplayThirdView(false);
                        // build array with items
                        var items = [];
                        var firstUnmappedColumnName = "";
                        var count = 0;
                        var unmappedCount = 0;
                        //found unmapped column

                        for (var i = 0; i < ModelFields.Mapping.Columns.length; i++) {
                            var item = ModelFields.Mapping.Columns[i];
                            if (typeof (item.MappedField) == "undefined" || item.MappedField == null) {
                                firstUnmappedColumnName = item.ColumnName;
                                break;
                            }
                        }

                        for (var i = 0; i < ModelFields.Mapping.Columns.length; i++) {
                            var item = ModelFields.Mapping.Columns[i];
                            var newItem = {
                                ColumnName: item.ColumnName,
                                IsIgnored: item.IsIgnored,
                                PossibleGroups: item.PossibleGroups,
                                PossibleFieldsInOrder: item.PossibleFieldsInOrder,
                                MappedField: item.MappedField,
                                IsActive: item.ColumnName == firstUnmappedColumnName ? true : false
                            };
                            items.push(newItem);
                            if (typeof (item.MappedField) == "undefined" || item.MappedField == null) {
                                unmappedCount = unmappedCount + 1;
                            }
                        }
                        MappingModel.MappingColumns(items);
                        MappingModel.TotalMatchedHeaders(ModelFields.Mapping.Columns.length - unmappedCount);
                        MappingModel.TotalHeaders(ModelFields.Mapping.Columns.length);
                        MappingModel.UpdateView();
                    }
                    else {
                        if (ModelFields.IsError == true)
                            showCustomError(eval("(" + ModelFields.ErrorMessage + ")"));
                    }
                },
                error: function (xhr, status, error) {
                    $.fancybox.close();
                    showCustomError(eval("(" + xhr.responseText + ")"));
                }
            });

        };
    };

};